package Classes.GameViews
{	
	import Classes.GameDatas.GD_Building_Func;
	import Classes.GameDatas.GD_Building_Sundry;
	import Classes.GameDatas.GD_Character;
	import Classes.GameDatas.GD_HavenWall;
	import Classes.GameDatas.GD_Map;
	import Classes.GameDatas.GD_MapGrid;
	import Classes.GameDatas.GD_Player;
	import Classes.GameDatas.GD_Reward;
	import Classes.GameDatas.GD_SickMan;
	import Classes.GameDatas.GD_Zombie;
	import Classes.GameErrors.GameError;
	import Classes.GameRecords.GR_Build;
	import Classes.GameUtils.GU_DataTransform;
	import Classes.GameUtils.GU_IsoUtils;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_Building;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_Building_Sundry;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_HavenWall;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MMT_Character;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapMoveTarget;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapNPCs.MN_SickMan;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapNPCs.MN_Zombie;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapTarget;
	import Classes.GameViews.ViewDisplayers.VD_Reward;
	import Classes.Interfaces.I_Bar;
	import Classes.Interfaces.I_ControlTarget;
	import Classes.Interfaces.I_FindingTarget;
	import Classes.Managers.M_ConfigData;
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.FileFilter;
	import flash.text.TextField;
	import flash.utils.Timer;

	
	public class myLLK
	{
		public 		var 	timerMax:uint=300;  		//秒
		public 		var 	timerNow:uint=timerMax;		
		public 		var 	timerGame:Timer;
		public 		var 	timerTxt:TextField;
		
		public 		var 	myLLK_zombieObj:Object;
		private 	var 	dis:uint=5;					//按格子数
		public 		var	 	mapAry2:Array;
		public 		var 	han:uint=8;
		public 		var 	lie:uint=8;
		public 		var 	gv_map:GV_Map;
		public 		var		zombieTypeAry:Array;
		public     var     zombieTypeNum:Object;
		private    var 	state:String="关闭";
		private    var 	offsetPoint:Point;
		private    var 	jss:Array;
		
		private    var     old:Object;
		
		private var oldTargetPoint:Point;
		private var nowSelectJs:myLLK_zombie;
		private var nowSelectJs2:myLLK_zombie;
		
		private var alljs:Number	= (han-2)*(lie-2);
		
		public function myLLK(map:GV_Map)
		{
			jss		=	new Array();
			gv_map	=	map;
			myLLK_zombieObj		=	new Object;
			keyStart();
			offsetPoint 	=	new Point(50,50);
		}
		
		public function init():void
		{
			zombieTypeAry		=   [20100001,20100002,20100003,20100004,20100005,20200001,20200002,20200003,20200004,20300001,20300002];
			var zombieNum:Array	=   [		4,		 4,       4,       4,       4,		 2,		  2,	   4,		2,       4,		  2];
			zombieTypeNum		=	new Object;
			for(var i:uint=0; i<zombieTypeAry.length; i++)
			{
				zombieTypeNum[zombieTypeAry[i]]		=	zombieNum[i];
				trace(zombieTypeNum[zombieTypeAry[i]]);
			}
			trace("jss=",alljs);
			setZombieGD();
			clear();
			reset();
			
			//this.gv_map.addEventListener(MouseEvent.CLICK,onclick);

			var isoPoint:Point=new Point( (offsetPoint.x)*GV_Map.instance.gridSize,(offsetPoint.y)*GV_Map.instance.gridSize);
			var screenPoint:Point=GU_IsoUtils.isoToScreen(isoPoint);

			//gv_map.stage.displayState="fullScreen";
			oldTargetPoint	=	new Point(gv_map.controlTarget.mapGrid.atx,gv_map.controlTarget.mapGrid.aty);
			old		=	{x:gv_map.x,y:gv_map.y};
			gv_map.controlTarget			
			TweenLite.to(gv_map,1.5,{x:-(screenPoint.x+(lie*dis)/2+gv_map.layerMapTargets.x),y:-(screenPoint.y+gv_map.layerMapTargets.y)});
			//TweenLite.to(gv_map,1.5,{x:-gu_hltosc(4,0).x,y:-gu_hltosc(4,0).y});
			
			trace(-screenPoint.x,-screenPoint.y,gv_map.x,gv_map.y);
		}
		public function onclick(e:Event):void
		{
			gv_map.controlTarget.setMove(oldTargetPoint);
			//e.stopPropagation();
		}
		/**
		 *创建僵尸 
		 * @param playerLv 僵尸数据存储器
		 * 
		 */		
		public function creatZombie(zombieData:GD_Zombie,pt:Point):myLLK_zombie
		{
				var zombieDisplayer:myLLK_zombie= new myLLK_zombie(zombieData,this);
				var randomGrid:GD_MapGrid=gv_map.getCoordinateGrid(pt);
				gv_map.addTarget(zombieDisplayer,randomGrid);
				return zombieDisplayer;
		}
		
		public function setZombieGD():void
		{
			for(var i:uint=0; i<zombieTypeAry.length; i++)
			{
				myLLK_zombieObj[zombieTypeAry[i]]		=		new GD_Zombie(zombieTypeAry[i]);
			}
		}
		
		public function reset():void
		{
			mapAry2 = new Array;
			var jscount:Object = new Object;
			for(var i:uint=0; i<zombieTypeAry.length; i++)
			{
				jscount[zombieTypeAry[i]]=0;
			}
			
			for(var i:uint=1; i<han-1; i++)
			{
				mapAry2[i]	=	new Array;
				mapAry2[i][0] = -1;
				for(var j:uint=1; j<lie-1; j++)
				{
					mapAry2[i][j]	=	zombieTypeAry[uint(Math.random()*zombieTypeAry.length)];
					var zb:myLLK_zombie = creatZombie( myLLK_zombieObj[ mapAry2[i][j] ], new Point(offsetPoint.x+j*dis,offsetPoint.y+i*dis) );
					jss.push( zb );
					zb.type	= mapAry2[i][j];
					zb.han		=	i;
					zb.lie		=	j;
					
					jscount[mapAry2[i][j] ]++;
					if(jscount[mapAry2[i][j] ] >= zombieTypeNum[mapAry2[i][j]]){
						zombieTypeAry.splice(zombieTypeAry.indexOf(mapAry2[i][j]),1);
					trace(zombieTypeAry.length);}
						
					
				}
				
				mapAry2[i][j]	=	-1;
			}

			mapAry2[0] = new Array;
			for(var i1:uint=0; i1<lie; i1++)
				mapAry2[0][i1] = -1;
			mapAry2[han-1] = new Array;
			for(i1=0; i1<lie; i1++)
				mapAry2[han-1][i1] = -1;
			
		}
		
		private var keyAry:Array=new Array;
		private var codeAry:Array=[108,108,107];
		public function keyStart():void
		{
			gv_map.stage.addEventListener(KeyboardEvent.KEY_DOWN,downHandler);
		}

		public function downHandler(e:KeyboardEvent):void
		{
				if(e.charCode==codeAry[keyAry.length])
				{
					keyAry.push(e.charCode);
				}
				else{
					keyAry.length=0;
					
				}
				if(keyAry.length>=codeAry.length){
				  keyAry.length=0;
				  switch(state)
				  {
					  case "关闭":
						  state	= "开启";
						  init();
						  break;
					  case "开启":
						  state="关闭";
						  stop();
						  break;
				  }
				}
//                trace(e.charCode);	
		}
		
		public function stop():void
		{
			for(var i:uint=0; i<jss.length; i++){
				if(gv_map.layerMapTargets.contains(jss[i] as MapTarget))
				gv_map.layerMapTargets.removeChild(jss[i] as MapTarget);
	
			}
			jss.length=0;
			
			for(i=0; i<gv_map.layerMapTargets.numChildren; i++)
				gv_map.layerMapTargets.getChildAt(i).visible=true;
			
			for(i=0; i<gv_map.layerSundries.numChildren; i++)
				gv_map.layerSundries.getChildAt(i).visible=true;
			//gv_map.stage.displayState="normal";
	
	

			
			TweenLite.to(gv_map,1.5,{x:old.x,y:old.y});
			this.gv_map.removeEventListener(MouseEvent.CLICK,onclick);

		}
		
		public function clear():void
		{
			
			for(var i:uint=0; i<gv_map.layerSundries.numChildren; i++)
				gv_map.layerSundries.getChildAt(i).visible=false;
			for(var i:uint=0; i<gv_map.layerMapTargets.numChildren; i++)
				gv_map.layerMapTargets.getChildAt(i).visible=false;
		}
	
		private var overMove:Boolean=false;
		public function clickZombie(zombie:myLLK_zombie):Array
		{
			
			//TweenLite.to(gv_map,1.5,{x:-(zombie.x+gv_map.layerMapTargets.x),y:-(zombie.y+gv_map.layerMapTargets.y)});
			
			if(overMove)return null;
			
			var array:Array;
//			trace("对比僵尸:",nowSelectJs,zombie);
			if(nowSelectJs==null)
			{
				nowSelectJs	=	zombie;
				nowSelectJs2=null;
				nowSelectJs.isSelectllk	=	true;
				return null;
			}
			else{
				if( nowSelectJs.type ==	zombie.type && nowSelectJs!=zombie){
					array	=	getLlkAry(zombie);
					if(array!=null){
						nowSelectJs2 = zombie;
						trace("结果"+array);

						overMove=true;
						LLKTweenLite(array);
						return null;
					}
				}
				nowSelectJs.isSelectllk	= false;
				nowSelectJs	 =null;
				nowSelectJs2 = null;
				
			}
		  return null;
		}
		
		public function getLlkAry(zombie:myLLK_zombie):Array
		{
			var returnAry:Array	=new Array;
			var pt1:Point	=   new Point(nowSelectJs.lie,nowSelectJs.han);
			var pt2:Point	=	new Point(zombie.lie,zombie.han);
			var xdis:Number		=	pt1.x-pt2.x;
			var ydis:Number		=	pt1.y-pt2.y;
			var xs:Number	=	xdis>=0?-1:1;
			var ys:Number	=	ydis>=0?-1:1;
			var type:uint	=   zombie.type;
			var pt:Point	=   new Point;
			var testAry:Array;//= new Array;
//			trace("对比:",pt1,pt2);
			returnAry.push(pt1.clone());
			
			if(xdis==0){
				if( null!= (pt=for1(false,pt1.clone(),pt2)) ){returnAry.push(pt.clone());return returnAry;}
				
				if(ys>0)testAry=["down","left","right"];
				else testAry=["up","left","right"];
			}
			else
			if(ydis==0){
				
				if( null!= (pt=for1(true,pt1.clone(),pt2)) ){returnAry.push(pt.clone());return returnAry;}
				
				if(xs>0)testAry=["right","up","down"];
				else testAry=["left","up","down"];
			}
			else
			{
				var ary2:Array	=	for2(true,pt1,pt2);
				if(null==ary2)	ary2	=	for2(false,pt1.clone(),pt2.clone());
				if(null!=ary2){while(ary2.length)returnAry.push(ary2.shift());returnAry.shift();return returnAry};
				
					if((xs+ys)==2)testAry=["right","down","up","left"];
					else
					if((xs+ys)==-2)testAry=["left","up","right","down"];
						else
						if(xs>0)testAry=["up","right","left","down"];
						else testAry=["down","left","up","right"];
			}
				
			var ptAry:Array;
			for(var i:uint=0; i<testAry.length; i++)
			{
				ptAry =	for3(testAry[i],pt1,pt2);
				if(ptAry!=null){
					while(ptAry.length)returnAry.push(ptAry.shift());
					return returnAry;
				}
			}
		
			
			return null;
		}
		
		private function bjpt(p1:Point,p2:Point):Boolean
		{
			if(p1.x==p2.x && p1.y==p2.y)return true;
			else return false;
		}
		
		
		public function for1(isX:Boolean,p1:Point,p2:Point):Point
		{
//			trace("for1",isX=="true"?"x":"y",p1,p2);

			if(isX){
				for(var x:Number=p1.x+1; x<lie; x++)
				{
					if(bjpt(new Point(x,p1.y),p2) ){
						return p2;
					}
					if(mapAry2[p1.y][x]!=-1)break;
				}
				for(var x:Number=p1.x-1; x>=0; x--)
				{
					if(bjpt(new Point(x,p1.y),p2) ){
						return p2;
					}
					if(mapAry2[p1.y][x]!=-1)break;
				}
				
				return null;
				
			}
			else{
				for(var y:Number=p1.y+1; y<han; y++)
				{
					if(bjpt(new Point(p1.x,y),p2) ){
						return p2;
					}
					if(mapAry2[y][p1.x]!=-1)break;
				}
				for(var y:Number=p1.y-1; y>=0; y--)
				{
					if(bjpt(new Point(p1.x,y),p2) ){
						return p2;
					}
					if(mapAry2[y][p1.x]!=-1)break;
				}
				
				return null;
				
			}
			
		}		
	
		public function for2(isX:Boolean,p1:Point,p2:Point):Array
		{
//			trace("for2",isX==true?"x":"y",p1,p2);
			var returnAry:Array = new Array;
			var dx:Number	= p1.x - p2.x;
			var dy:Number	= p1.y - p2.y;
			var xdis:Number		=  dx >0?-1:1;
			var ydis:Number 	=  dy >0?-1:1;

			returnAry.push(p1.clone());
			
			if(isX){
				var x:Number;
				var y:Number;
				
				x=p1.x;
				do{
					x+=xdis;
					if(mapAry2[p1.y][x]!=-1)return null;
				}while(x!=p2.x&&x>-1&&x<lie)

				returnAry.push(new Point(p2.x,p1.y));		
				y=p1.y;
				do{
					y+=ydis;
					if(bjpt(new Point(p2.x,y),p2) )
					{
						returnAry.push(new Point(p2.x,y));
						return returnAry;
					}
					if(mapAry2[y][p2.x]!=-1)return null;

				}while(y!=p2.y&&y>-1&&y<han)

				
				returnAry.push(p2.clone());
				return returnAry;
			}
			else{
				var x:Number;
				var y:Number;
				y=p1.y;
				do{
					y+=ydis;
					if(mapAry2[y][p1.x]!=-1)return null;

				}while(y!=p2.y&&y>-1&&y<han)

				
				returnAry.push(new Point(p1.x,p2.y));	
				x=p1.x;
				do{
					x+=xdis;
					if(bjpt(new Point(x,p2.y),p2) )
					{
						returnAry.push(new Point(x,p2.y));
						return returnAry;
					}
					if(mapAry2[p2.y][x]!=-1)return null;

				}while(x!=p2.x&&x>-1&&x<lie)

				returnAry.push(p2.clone());
				return returnAry;

			}
		}		
		
		public function for3(dir:String,p1:Point,p2:Point):Array
		{
//			trace("for3",dir,p1,p2);
			var returnAry:Array = new Array;
			var ptary:Array;
	
			switch(dir)
			{
				case "up":
					var y:Number;
					for(y=p1.y-1; y>=0; y--)
					{
						if(mapAry2[y][p1.x]!=-1)return null;
						ptary	=	for2(true,new Point(p1.x,y),p2);
						if(null!=ptary){
//							returnAry.push(new Point(p1.x,y));
							while(ptary.length)returnAry.push(ptary.shift());
							return returnAry;
						}
					}
					return null;
				case "down":
					var y:Number;
					for(y=p1.y+1; y<han; y++)
					{
						if(mapAry2[y][p1.x]!=-1)return null;
						ptary	=	for2(true,new Point(p1.x,y),p2);
						if(null!=ptary){
//							returnAry.push(new Point(p1.x,y));
							while(ptary.length)returnAry.push(ptary.shift());
							return returnAry;
						}
					}
					return null;
				case "left":
					var x:Number;
					for(x=p1.x-1; x>=0; x--)
					{
						if(mapAry2[p1.y][x]!=-1)return null;
						ptary	=	for2(false,new Point(x,p1.y),p2);
						if(null!=ptary){
//							returnAry.push(new Point(x,p1.y));
							while(ptary.length)returnAry.push(ptary.shift());
							return returnAry;
						}
					}
					return null;
				case "right":
					var x:Number;
					for(x=p1.x+1; x<lie; x++)
					{
						if(mapAry2[p1.y][x]!=-1)return null;
						ptary	=	for2(false,new Point(x,p1.y),p2);
						if(null!=ptary){
//							returnAry.push(new Point(x,p1.y));
							while(ptary.length)returnAry.push(ptary.shift());
							return returnAry;
						}
					}
					return null;	
					
			}
			
			return null;
		}
		
		private var okAry:Array;
		private var numTween:Number=1;
		private var toSp:Sprite;
		public function myfun():void
		{
			if(numTween>=okAry.length){
				this.gv_map.removeChild(toSp);
				
			}
			else
			{
				var isoPoint:Point=new Point(okAry[numTween].x*this.dis*GV_Map.instance.gridSize,okAry[numTween].y*this.dis*GV_Map.instance.gridSize);
				var screenPoint:Point=GU_IsoUtils.isoToScreen(isoPoint);
				var thePoint:Point = new Point(screenPoint.x,screenPoint.y);
				
				TweenLite.to(toSp,1,{x:thePoint.x,y:thePoint.y,onComplete:myfun});
				numTween++;
			}
		}
		
		
		
		
		var tosp:Sprite;
		var okary:Array=new Array;
		public function LLKTweenLite(ary:Array):void
		{
			okary.length = 0;
			tosp = new Sprite;
			tosp.graphics.beginFill(0xff0000);
			tosp.graphics.drawCircle(0,0,10);
			tosp.graphics.endFill();
			

			for(var i:uint=0; i<ary.length; i++)
			{
				var tp:Point	=	gu_hltosc(ary[i].y,ary[i].x);
				tp.x+=5;
				tp.y-=20;
				okary.push(tp);
			}

			
			var tp:Point	=	okary.shift();
			tosp.x = tp.x;
			tosp.y = tp.y;
			gv_map.addChild(tosp);

			var tp:Point	=	okary.shift();
			TweenLite.to(tosp,0.5,{x:tp.x,y:tp.y,onComplete:TLto});
			//sp.x = gu_hltosc(3,3).x;
			//sp.y = gu_hltosc(3,3).y;
		}

		public function TLto():void
		{
			
			if(okary.length<1){
				gv_map.removeChild(tosp);
				overjs1 = nowSelectJs;
				overjs2 = nowSelectJs2;
				TweenLite.to(nowSelectJs,2,{x:gu_hltosc(0,0).x,y:gu_hltosc(0,0).y-200,rotation:360,onComplete:theover});
				TweenLite.to(nowSelectJs2,2,{x:gu_hltosc(0,0).x,y:gu_hltosc(0,0).y-200,rotation:360,onComplete:theover});
				nowSelectJs = nowSelectJs2 =null
			}
			else
			{
				var tp:Point	=	okary.shift();
				TweenLite.to(tosp,0.5,{x:tp.x,y:tp.y,onComplete:TLto});
			}
		}
		
		private var overjs1:myLLK_zombie;
		private var overjs2:myLLK_zombie;
		public function theover():void
		{
			if(overjs1!=null){
			overjs1.over();
			overjs1 = null;
			}
			
			if(overjs2!=null){
				overjs2.over();
				overjs2 = null;
			}
			
			if(overjs1==null && overjs2==null)overMove=false;
			
			
		}
		public function gu_hltosc(han1:Number,lie1:Number):Point
		{
			var np:Point = new Point(offsetPoint.x+lie1*dis,offsetPoint.y+han1*dis);
			var grid:GD_MapGrid=gv_map.getCoordinateGrid(np);
			var isoPoint:Point=new Point(grid.atx*GV_Map.instance.gridSize,grid.aty*GV_Map.instance.gridSize);
			return GU_IsoUtils.isoToScreen(isoPoint);
		}

	}
}